package tw.com.softleader.fire.setting.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import tw.com.softleader.fire.common.dao.GenericDao;
import tw.com.softleader.fire.setting.entity.AcuAddrEntity;

/**
 * @Author:Rhys
 */

@Repository
public class AcuAddrDao extends GenericDao<AcuAddrEntity> {

	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public List<AcuAddrEntity> findByAcuNo(AcuAddrEntity entity){
		
		Session session = sessionFactory.getCurrentSession();
		StringBuffer sql = new StringBuffer();
		
		sql.append("select * from set_acu_addr where acu_no ilike :acuNo");
		Query query = session.createSQLQuery(
				sql.toString()).addEntity(AcuAddrEntity.class);
		
		if( StringUtils.isEmpty(entity.getAcuNo()) ){
			entity.setAcuNo("%");
		} else {
			entity.setAcuNo("%"+entity.getAcuNo()+"%");
		}
		List<AcuAddrEntity> models = query.setString("acuNo", entity.getAcuNo()).list();
		return models;
	}
	
}
